package com.stained.white.goat.random;

import java.util.Random;

/**
 * X-OR Shift乱数生成クラス.
 * @author stained white goat
 *
 */
public class ExclusiveOrShift extends Random {

    /** serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /** x seed. */
    private static int x;
    /** y seed. */
    private static int y;
    /** z seed. */
    private static int z;
    /** w seed. */
    private static int w;

    /**
     * Constructor.
     */
    public ExclusiveOrShift() {
        // ナノ秒の16bitをシード化
        this((int) System.nanoTime());
    }

    /**
     * Constructor.
     * @param seed seed
     */
    public ExclusiveOrShift(int seed) {
        w = seed;
        x = seed << 16 + seed >> 16;
        y = w + x;
        z = x ^ y;
    }

    @Override
    public int nextInt(int n) {
        int t = x ^ (x << 11);
        x = y;
        y = z;
        z = w;
        w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
        // 乱数範囲指定 0 - n
        return w % (n + 1);
    }
}
